<?php


class MyFacebook {

  private static $instance;
  
  private function __construct() {
  }
  
  // The singleton method
  public static function getInstance() {
    if (!isset(self::$instance)) {
      $c = __CLASS__;
      self::$instance = new Facebook(array(
        'appId'  => FACEBOOK_APP_ID,
        'secret' => FACEBOOK_SECRET,
        'cookie' => true,
      ));
    }
    return self::$instance;
  }
  
  
  public static function get_facebook_cookie() {
    $app_id = FACEBOOK_APP_ID;
    $application_secret = FACEBOOK_SECRET;
    
    $args = array();
    $cookieName= 'fbs_' . $app_id;
    if (key_exists($cookieName, $_COOKIE)) {
      parse_str(trim($_COOKIE[$cookieName], '\\"'), $args);
      ksort($args);
      $payload = '';
      foreach ($args as $key => $value) {
        if ($key != 'sig') {
          $payload .= $key . '=' . $value;
        }
      }
      if (md5($payload . $application_secret) != $args['sig']) {
        return null;
      }
      return $args;
    } else {
      return null;
    }
  }
  
}
